/**
 * Project Name:gc.sms.mvn
 * File Name:BaseServiceImpl.java
 * Package Name:com.clps.dev.sms.common.service.impl
 * Date:2018-11-28 17:34:07
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.common.service.impl;

import java.util.List;

import com.clps.dev.sms.common.dao.IBaseDao;
import com.clps.dev.sms.common.dao.impl.BaseDaoImpl;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.common.service.IBaseService;
import com.clps.dev.sms.util.page.PageBean;
import com.clps.dev.sms.util.result.ResponseResultUtil;

/**
 * ClassName: BaseServiceImpl <br/>
 * Description: BaseServiceImpl模板
 *
 * @author Jack.Huang
 * @version V1.0
 * @since JDK 1.8
   @date:2018-11-28 17:34:07
 */

public class BaseServiceImpl<T> implements IBaseService<T> {
	
	
	private Class<?> clazz;
	public BaseServiceImpl(Class<?> clazz) {
		super();
		this.clazz = clazz;
	}
	
	
	private IBaseDao<T> baseDao = new BaseDaoImpl<>(clazz);
	
	private PageBean<T> pageBean = new PageBean<>();
	

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPage(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public ResponseResult<T> queryByPage(Integer currPage, Integer pageSize) {
		PageBean<T> pageBean = new PageBean<>();
		pageBean.setCurrPage(currPage);
		pageBean.setPageSize(pageSize);
		Integer totalCount = baseDao.findCount();
		pageBean.setTotalCount(totalCount);
		Double Count = totalCount.doubleValue();
		Integer totalPage = (int) Math.ceil(Count / pageSize);
		pageBean.setTotalPage(totalPage);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<T> list = baseDao.findByPage(begin, pageSize);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPageTime(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public ResponseResult<T> queryByPageTime(Integer currPage, Integer pageSize,
			String time) {
		pageBean = queryByPageCommon(currPage, pageSize);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<T> list = baseDao.findByPageOrderByTime(begin, pageSize, time);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPageName(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public ResponseResult<T> queryByPageName(Integer currPage, Integer pageSize,
			String name) {
		pageBean = queryByPageCommon(currPage, pageSize);
		Integer begin = (pageBean.getCurrPage() - 1) * pageBean.getPageSize();
		List<T> list = baseDao.findByPageOrderByName(begin, pageSize, name);
		pageBean.setList(list);
		return ResponseResultUtil.success(ResultType.PAGEINFO, pageBean);
	}


	/**
	 * 抽取公共方法
	 * @see com.clps.dev.sms.common.service.IBaseService#queryByPageCommon(java.lang.Integer, java.lang.Integer)
	 */
	public PageBean<T> queryByPageCommon(Integer currPage, Integer pageSize) {
		PageBean<T> pageBean = new PageBean<>();
		pageBean.setCurrPage(currPage);
		pageBean.setPageSize(pageSize);
		Integer totalCount = baseDao.findCount();
		pageBean.setTotalCount(totalCount);
		Double Count = totalCount.doubleValue();
		Integer totalPage = (int) Math.ceil(Count / pageSize);
		pageBean.setTotalPage(totalPage);
		return pageBean;
	}

}
